/*
 * @Author: 0x9DEFA478
 * @Date: 2024-08-02 20:00:50
 * @LastEditTime: 2024-08-12 20:31:55
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "./H_TS_Semaphore.h"
#include "../LL/H_TS_LL.h"




void H_TS_Semaphore_Init(H_TS_Semaphore* Semaphore,Husize MaxValue,Husize InitialValue){

  oH_TS_Assert_HandleValid(Semaphore==NULL,"H_TS:H_TS_Semaphore_Init():空的句柄");
  oH_TS_Assert_ParamValid(MaxValue<1,"H_TS:H_TS_Semaphore_Init():MaxValue不能小于1");
  oH_TS_Assert_ParamValid(InitialValue>MaxValue,"H_TS:H_TS_Semaphore_Init():InitialValue不能大于MaxValue");

  H_TS_Event_Init(&Semaphore->ReleaseEvent);
  H_TS_Lock_Init(&Semaphore->ReleaseLock);
  H_TS_Event_Init(&Semaphore->AcquireEvent);
  H_TS_Lock_Init(&Semaphore->AcquireLock);

  Semaphore->Value=InitialValue;
  Semaphore->MaxValue=MaxValue;
}

int H_TS_Semaphore_Deinit(H_TS_Semaphore* Semaphore){

  oH_TS_Assert_HandleValid(Semaphore==NULL,"H_TS:H_TS_Semaphore_Deinit():空的句柄");

  if((0!=H_TS_Event_Deinit(&Semaphore->ReleaseEvent))
    ||(0!=H_TS_Lock_Deinit(&Semaphore->ReleaseLock))
    ||(0!=H_TS_Event_Deinit(&Semaphore->AcquireEvent))
    ||(0!=H_TS_Lock_Deinit(&Semaphore->AcquireLock))){
    oH_TS_Crash("H_TS:H_TS_Semaphore_Deinit():子部件被占用");
    for(;;){
    }
  }

  return 0;
}

H_TS_Semaphore* new_H_TS_Semaphore(Husize MaxValue,Husize InitialValue){
  H_TS_Semaphore* r;

  oH_TS_LL_SchedulerSuspend();
  r=H_TS_LL_Core_Instance.Memory.Malloc(sizeof(H_TS_Event));
  oH_TS_LL_ResumeSchedulerAuto();
  if(r==NULL){
    return r;
  }

  H_TS_Semaphore_Init(r,MaxValue,InitialValue);

  return r;
}

int delete_H_TS_Semaphore(H_TS_Semaphore* Semaphore){

  if(0!=H_TS_Semaphore_Deinit(Semaphore)){
    return -1;
  }

  oH_TS_LL_SchedulerSuspend();
  H_TS_LL_Core_Instance.Memory.Free(Semaphore);
  oH_TS_LL_ResumeSchedulerAuto();

  return 0;
}



